﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProDivingAdmin2salmon14
{
    public class DiverList : BindingList<Diver>, IDiverList
    {

        public DiverList()
        {
 
        }

        public DiverList(string filePath) 
        {
            foreach (var diver in FileManager.LoadDivers(filePath)) 
            {
                this.Add(diver);
            }
        }

        public DiverList(List<Diver> diverList)
        {
            foreach (var diver in diverList)
            {
                this.Add(diver);
            }
        }

        public void CalculateScore(Diver diver) 
        {
            diver.CalculateScore();
        }

        public void CalculateAllDivers() 
        {
            foreach (var diver in this) 
            {
                diver.CalculateScore();
            }
        }

        public void Shuffle()
        {
            Random rng = new Random();
            int n = this.Count;
            while (n > 1)
            {
                n--;
                int k = rng.Next(n + 1);
                Diver value = this[k];
                this[k] = this[n];
                this[n] = value;
            }
        }

        public void DisqualifyDiver(Diver diver)
        {
            foreach (var diverFromList in this)
            {
                if (diverFromList.ID == diver.ID)
                {
                    diverFromList.Disqualify();
                }
            }
        }

        public void MarkDiverAsInjured(Diver diver)
        {
            foreach (var diverFromList in this)
            {
                if (diverFromList.ID == diver.ID)
                {
                    diverFromList.MarkAsInjured();
                }
            }
        }

        public void DeleteDiver(int diverIndx)
        {
            this.RemoveAt(diverIndx);
        }
    }
}
